<?php
/**
 * 后台管理员操作处理代码
 */
// 引入项目初始化文件
require '../include/init.php';
// 获取操作的标识act的值
$act = isset($_GET['act']) ? $_GET['act'] : $_POST['act'];
// 处理非法操作
$action = ['add', 'edit','del'];
if(!in_array($act, $action)){
	msg('非法操作');
}

//商品类添加
else if($act == 'add'){
	//print_r($_POST);
	extract($_POST);
	//数据处理
	 $cate_name = trim($cate_name);

	if($parent_id ==0){
		$level = 1;
	}else{
		$sql = "SELECT * from tk_product_category where cate_id = $parent_id";
		$res = mysql_query($sql);
		$info = mysql_fetch_assoc($res);
		$level = $info['level']+1;
	}
	//数据库操作
	$sql = "INSERT into  tk_product_category(cate_name,parent_id,level) values ('$cate_name','$parent_id','$level')";

	$res = mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('添加分类成功','product_cate_list.php');
	}else{
		msg('添加分类失败！','product_cate_add.php');
	}

}
//商品类修改
else if($act == 'edit'){
	//print_r($_POST);
	extract($_POST);
	//数据处理
	$cate_name =trim($cate_name);
	//判断该分类下面 有没有自分类
	$sql = "SELECT * FROM tk_product_category where parent_id =$id ";
	$re = mysql_query($sql);
	if(mysql_num_rows($re)>0){
		//有子分类 只可以修改分类名
		$sql = "UPDATE tk_product_category set cate_name = $cate_name where cate_id =$id";
		mysql_query($sql);
		if(mysql_affected_rows()>0){
			msg('修改成功！','product_cate_list.php');
		}else{
			msg('有子分类不能修改','product_cate_edit.php');
		}
	}else{
		//没有子分类 可以修改分类名，上级分类，级别
		if($parent_id ==0){
			$level =1;
		}else{
			$sql = "SELECT * FROM  tk_product_category where cate_id =$parent_id";
			$re = mysql_query($sql);
			$info = mysql_fetch_assoc($re);
			$level = $info['level'] +1;
		}
		$sql ="UPDATE tk_product_category set cate_name = '$cate_name',parent_id=$parent_id,level=$level where cate_id =$id ";
		mysql_query($sql);
		if(mysql_affected_rows()>0){
			msg('修改成功！','product_cate_list.php');
		} else{
			msg('修改失败','product_cate_edit.php');
		}
	}
}
//商品类删除
else if($act == 'del'){
	extract($_GET);
	//$sql = "SELECT * FROM  tk_new_category"
	$sql = "SELECT * FROM tk_product_category where parent_id =$id ";
	$re = mysql_query($sql);
	if(mysql_num_rows($re)>0){
		msg('有子分类不能删除','product_cate_list.php');
		exit;
	}
	$sql = "DELETE FROM  tk_product_category where cate_id =$id";
	mysql_query($sql);
	if(mysql_affected_rows()>0){
		header("location:product_cate_list.php");
	}else{
		msg('删除失败','product_cate_list.php');
	}
}
?>